﻿using System;

using Microsoft.VisualStudio;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Package;
using Microsoft.VisualStudio.TextManager.Interop;

namespace Microsoft.WowAddonStudio.Lua.LanguageService.Model
{
    public class LuaViewFilter : ViewFilter
    {
        public LuaViewFilter(CodeWindowManager mgr, IVsTextView view) : base(mgr, view)
        {
        }

        /// <summary>
        /// Queries the command status.
        /// </summary>
        /// <param name="guidCmdGroup">The GUID CMD group.</param>
        /// <param name="nCmdId">The n CMD id.</param>
        /// <returns></returns>
        protected override int QueryCommandStatus(ref Guid guidCmdGroup, uint nCmdId)
        {
            if (guidCmdGroup == VSConstants.GUID_VSStandardCommandSet97)
            {
                switch ((VSConstants.VSStd97CmdID)nCmdId)
                {
                    case VSConstants.VSStd97CmdID.GotoRef:
                        return (int)(OLECMDF.OLECMDF_SUPPORTED);
                    case VSConstants.VSStd97CmdID.RunToCursor:
                        return (int)(OLECMDF.OLECMDF_SUPPORTED | OLECMDF.OLECMDF_INVISIBLE);
                }
            }

            if (guidCmdGroup == VSConstants.VSStd2K)
            {
                if (nCmdId == (uint)VSConstants.VSStd2KCmdID.INSERTSNIPPET)
                    return (int)(OLECMDF.OLECMDF_SUPPORTED | OLECMDF.OLECMDF_ENABLED);
            }

            return base.QueryCommandStatus(ref guidCmdGroup, nCmdId);
        }

        public override bool HandlePreExec(ref Guid guidCmdGroup, uint nCmdId, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            if (guidCmdGroup == VSConstants.VSStd2K)
            {
                if (nCmdId == (uint)VSConstants.VSStd2KCmdID.INSERTSNIPPET)
                {
                    ExpansionProvider expansionProvider = this.GetExpansionProvider();
                    if (this.TextView != null && expansionProvider != null)
                    {
                        expansionProvider.DisplayExpansionBrowser(this.TextView, "Insert Snippet", null, false, null, false);
                    }

                    return true;
                }
            }

            // Base class handled the command.  Do nothing more here.
            return base.HandlePreExec(ref guidCmdGroup, nCmdId, nCmdexecopt, pvaIn, pvaOut);
        }
    }
}